//
//  AHParagraphStyleSetting.swift
//  AttributedStringHelper
//
//  Created by JY_NEW on 2021/11/1.
//

import UIKit

public enum AHParagraphStyleSetting {
    case lineSpacing(CGFloat)
    case paragraphSpacing(CGFloat)
    case alignment(NSTextAlignment)
    case firstLineHeadIndent(CGFloat)
    case headIndent(CGFloat)
    case tailIndent(CGFloat)
    case lineBreak(NSLineBreakMode)
    case minLineHeight(CGFloat)
    case maxLineHeight(CGFloat)
    case direction(NSWritingDirection)
    case lineHeight(multiple:CGFloat)
    case paragraphSpacingBefore(CGFloat)
    case hyphenationFactor(Float)
    case tabStops([NSTextTab])
    case tab(interval:CGFloat)
    ///默认紧缩截断
    case tighteningTruncation(Bool)
    case add(tabStop:NSTextTab)
    case remove(tabStop:NSTextTab)
    case config(NSParagraphStyle)
    func apply(to style: NSMutableParagraphStyle) {
        switch self {
        case let .lineSpacing(value): style.lineSpacing = value
        case let .paragraphSpacing(value): style.paragraphSpacing = value
        case let .alignment(value): style.alignment = value
        case let .firstLineHeadIndent(value): style.firstLineHeadIndent = value
        case let .headIndent(value): style.headIndent = value
        case let .tailIndent(value): style.tailIndent = value
        case let .lineBreak(value): style.lineBreakMode = value
        case let .minLineHeight(value): style.minimumLineHeight = value
        case let .maxLineHeight(value): style.maximumLineHeight = value
        case let .direction(value): style.baseWritingDirection = value
        case let .lineHeight(multiple:value): style.lineHeightMultiple = value
        case let .paragraphSpacingBefore(value): style.paragraphSpacingBefore = value
        case let .hyphenationFactor(value): style.hyphenationFactor = value
        case let .tabStops(value): style.tabStops = value
        case let .tab(interval:value): style.defaultTabInterval = value
        ///默认紧缩截断
        case let .tighteningTruncation(value):
                style.allowsDefaultTighteningForTruncation = value
        case let .add(tabStop:value):
                style.addTabStop(value)
        case let .remove(tabStop:value):
                style.removeTabStop(value)
        case let .config(value):
                style.setParagraphStyle(value)
        }
    }
}
extension AHSetting {
    ///段落样式
    public static func paragraph(_ settings: AHParagraphStyleSetting...) -> Self {
        let style = NSMutableParagraphStyle()
        settings.forEach({ $0.apply(to: style) })
        return .attribute(.paragraphStyle, style)
    }
}
extension NSParagraphStyle {
    /// 创建段落样式
    public static func create(_ settings: AHParagraphStyleSetting...) -> NSMutableParagraphStyle {
        let style = NSMutableParagraphStyle()
        settings.forEach({ $0.apply(to: style) })
        return style
    }
}
